public class Work7 {
    public static void main(String[] args) {
        int[] array = {2, 2, 1, 1, 1, 2, 2};
        int count = 0;
        int i = 0;
        int ans = 0;
        for (; i < array.length; i++) {
            if (count == 0) {
                ans = array[i];
            }
            if (i == ans) {
                count++;
            } else {
                count--;
            }
        }

        System.out.println(ans);
    }
}
